<?php
class CPSLocation extends AppModel {
    var $name = 'CPSLocation';

    var $useTable = 'cust_product_site_locations';
    
    var $belongsTo = array(
        'CustProductSite', 'CustLocation'
    );


    /**
     * เพิ่ม default location ลง CPSLocation
     * กรณีที่ยังไม่มี cust_location สำหรับ cps_id ที่เลือก
     * @param <type> $cust_product_site_id
     * @param <type> $cust_location_id
     */
    function addLocation($cust_product_site_id, $cust_location_id) {

        // ถ้ายังไม่มี location นี้ใน cps นี้
        if (!$this->hasAny(array('cust_product_site_id' => $cust_product_site_id, 'cust_location_id' => $cust_location_id))) {
            $data['CPSLocation']['id'] = null;
            $data['CPSLocation']['cust_product_site_id'] = $cust_product_site_id;
            $data['CPSLocation']['cust_location_id'] = $cust_location_id;
            $this->save($data);
            return true;
        }
        return false;

    }
}
